package com.agou.ssh.web.jsp.tags;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.agou.core.ss.SSTrace;
import com.agou.core.util.UtString;

public class AjaxTag extends TagSupport {
	private static final long serialVersionUID = -7907663097387273568L;
	
	private static final SSTrace sstrace = SSTrace.getInstance(AjaxTag.class);

	private String async;//
	private String beforeSend;//
	private String cache;//
	private String complete;//
	private String contentType = "application/x-www-form-urlencoded";//
	private String data;//
	private String dataFilter;//
	private String dataType = "json";//
	private String error;//
	private String password;//
	private String success;//
	private String scriptCharset;//
	private String timeout = "3000";//
	private String type = "POST";//
	private String url;//
	private String username;//

	public String getAsync() {
		return async;
	}

	public void setAsync(String async) {
		this.async = async;
	}

	public String getBeforeSend() {
		return beforeSend;
	}

	public void setBeforeSend(String beforeSend) {
		this.beforeSend = beforeSend;
	}

	public String getCache() {
		return cache;
	}

	public void setCache(String cache) {
		this.cache = cache;
	}

	public String getComplete() {
		return complete;
	}

	public void setComplete(String complete) {
		this.complete = complete;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getDataFilter() {
		return dataFilter;
	}

	public void setDataFilter(String dataFilter) {
		this.dataFilter = dataFilter;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public String getScriptCharset() {
		return scriptCharset;
	}

	public void setScriptCharset(String scriptCharset) {
		this.scriptCharset = scriptCharset;
	}

	public String getTimeout() {
		return timeout;
	}

	public void setTimeout(String timeout) {
		this.timeout = timeout;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int doEndTag(){
		JspWriter out = pageContext.getOut();
		try {
			StringBuffer sbTag = new StringBuffer();
			sbTag.append("$.ajax({").append("\n");
			
			// (默认: true) 默认设置下，所有请求均为异步请求。如果需要发送同步请求，请将此选项设置为 false。注意，同步请求将锁住浏览器，用户其它操作必须等待请求完成才可以执行。
			if(this.isEmpoty(this.getAsync()) == false){
				if("false".equals(getAsync())){
					sbTag.append("async : \"false\" , \n");
				}else{
					sbTag.append("async : \"true\" , \n");
				}
			}
			
			// (默认: true,dataType为script时默认为false) jQuery 1.2 新功能，设置为 false 将不会从浏览器缓存中加载请求信息。
			if(this.isEmpoty(this.getAsync()) == false){
				sbTag.append("cache : \"").append(this.getCache()).append("\",\n");
			}
			// (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合
			if(this.isEmpoty(this.getContentType()) == false){
				sbTag.append("contentType : \"").append(this.getContentType()).append("\",\n");
			}
			
			if(this.isEmpoty(this.getData()) == false){
				//格式 {'key':'value','key':'value'}
				sbTag.append("data : ").append(this.getData()).append(" ,\n");
			}
			//预期服务器返回的数据类型 "xml"、"html"、"script"  "json"、"jsonp"、"text" 
			if(this.isEmpoty(this.getDataType()) == false){
				sbTag.append("dataType : \"").append(this.getDataType()).append("\",\n");
			}else{
				sbTag.append("dataType : \"json\",\n");
			}
			
			
			//用于响应HTTP访问认证请求的用户名
			if(this.isEmpoty(this.getUsername()) == false){
				sbTag.append("username : \"").append(this.getUsername()).append("\",\n");
			}
			//用于响应HTTP访问认证请求的密码
			if(this.isEmpoty(this.getPassword()) == false){
				sbTag.append("password : \"").append(this.getPassword()).append("\",\n");
			}
			//只有当请求时dataType为"jsonp"或"script"，
			if(this.isEmpoty(this.getScriptCharset()) == false){
				sbTag.append("scriptCharset : \"").append(this.getScriptCharset()).append("\",\n");
			}
			//请求方式 ("POST" 或 "GET")， 默认为 "POST"。
			if(this.isEmpoty(this.getType())){
				sbTag.append("type : \"POST\" , \n");
			}else{
				sbTag.append("type : \"").append(this.getType()).append("\" , \n");
			}
			//
			if(this.isEmpoty(this.getTimeout()) == false){
				sbTag.append("timeout : \"").append(this.getTimeout()).append("\",\n");
			}
			//
			if(this.isEmpoty(this.getScriptCharset ()) == false){
				sbTag.append("scriptCharset : \"").append(this.getScriptCharset()).append("\",\n");
			}
			
			//发送请求前可修改 XMLHttpRequest 对象的函数，如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数
			if(this.isEmpoty(this.getBeforeSend ()) == false){
				sbTag.append("beforeSend : ").append(this.getBeforeSend()).append( ",\n");
			}
			
			if(this.isEmpoty(this.getError()) == false){
				sbTag.append("error  : ").append(this.getError()).append(" ,\n");
			}
			
			/**
			 * 下边三个方法冲突，一次只能使用其中的一个方法
			 */
			if(this.isEmpoty(this.getSuccess())){//如果完成方法为空
				if(this.isEmpoty(this.getComplete())){
					if(this.isEmpoty(this.getDataFilter()) == false){
						sbTag.append("dataFilter : ").append(this.getSuccess()).append( ",\n");
					}
				}else{
					sbTag.append("complete : ").append(this.getSuccess()).append( ",\n");
				}
			}else{
				sbTag.append("success : ").append(this.getSuccess()).append( ",\n");
			}
			sbTag.append("url  : \"").append(this.getUrl()).append("\" \n");
			sbTag.append("});");
			out.print(sbTag.toString());
		} catch (Exception e) {
			sstrace.error("doEndTag", "调用Ajax标签",e);
			try {
				out.print(e.getMessage());
			} catch (IOException e1) {
				sstrace.error("doEndTag", "调用Ajax标签",e1);
				throw new java.lang.IllegalArgumentException("调用ajax标签异常:" + e.getMessage());
			}
		}
		return EVAL_PAGE;
	}
	
	
	private boolean isEmpoty(String value){
		if(UtString.isEmpty(value)){
			return true;
		}
		if(value.trim().equals("null")){
			return true;
		}
		return false;
	}
}
